<template>
  <body>
    <div class="img">
      <img :src="logo2" alt="" />
    </div>
    <div class="texiao">
      <HelloWorld />
    </div>
    <div class="login">
      
      <el-form
    ref="ruleFormRef"
    :model="LoginData"
    status-icon
    :rules="rules"
    label-width="120px"
  >
  <!-- class="demo-ruleForm" -->
    <el-form-item label="账号" prop="phone">
      <el-input v-model="LoginData.adminname" type="text" autocomplete="off" />
    </el-form-item>
    <el-form-item label="密码" prop="pass">
      <el-input
        v-model="LoginData.password"
        type="password"
        autocomplete="off"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="()=>submitForm(ruleFormRef)"
        >登录</el-button
      >
    </el-form-item>
  </el-form>
    </div>
  </body>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import logo2 from '../../assets/logo2.png'
// 导入封装好的请求
import { user_login } from "../../utils/api";
import { ElMessage, } from "element-plus";
import type { FormInstance } from 'element-plus'
import getRoutes from '../../permessions/route'
import "element-plus/dist/index.css";

// 需要跳转的页面引入useRouter
import { useRouter } from "vue-router";
// 定义rputer
const router = useRouter();
interface Iuser{
  adminname:string,
  password:string
}

// 管理员数据
const LoginData = reactive<Iuser>({
  adminname: "admin",
  password: "123456",
});
// 定义登录的正则
const ruleFormRef = ref<FormInstance>()
const validatePhone = (rule:any, value:string, callback:any) => {
  if (value === "") {
    callback(new Error("请输入账号"));
  } else if (/^[\w]{4,16}$/.test(LoginData.adminname) == false) {
    callback(new Error("用户名不符合规则"));
  } else {
    callback();
  }
};
const validatePass = (rule:any, value:string, callback:any) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  }else if(/^[\w]{6,}$/.test(LoginData.password)==false){
    callback(new Error("密码至少六位"));
  }
   else {
    callback();
  }
};
getRoutes()
const rules = ref({
  pass: [{ validator: validatePass, trigger: "change" }],
  phone: [{ validator: validatePhone, trigger: "change" }],
});
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    // 满足正则
    if (valid) {
      console.log('submit!')
      // 满足正则时发送请求
      user_login(LoginData).then((res) => {
      if (res.data.code === "200") {
        sessionStorage.setItem("token", res.data.data.token);
        console.log(res.data.data.checkedkeys)
        sessionStorage.setItem('checkedkeys',res.data.data.checkedkeys)
        sessionStorage.setItem('userInfo',LoginData.adminname)
        let token = sessionStorage.getItem("token");
        router.push("/HomeIndex");
      } else if (res.data.code === "10003") {
        ElMessage({
          message: "密码错误",
          type: "error",
        });
      } else {
        ElMessage({
          message: "未注册",
          type: "error",
        });
      }
    });
    } else {
      ElMessage({
        message:'用户名或密码不符规则',
        type:'error'
      })
      return false
    }
  })
   
  }
  // 键盘事件，判断是否按下回车
document.onkeyup=(event)=>{
  // var key=event.keyCode||event.which||event.charCode
  if(event.key=='Enter'){
    console.log('xx');
    user_login(LoginData).then((res) => {
      if (res.data.code === "200") {
        sessionStorage.setItem("token", res.data.data.token);
        console.log(res.data.data.checkedkeys)
        sessionStorage.setItem('checkedkeys',res.data.data.checkedkeys)
        sessionStorage.setItem('userInfo',LoginData.adminname)

        let token = sessionStorage.getItem("token");
        router.push("/HomeIndex");
      } else if (res.data.code === "10003") {
        ElMessage({
          message: "密码错误",
          type: "error",
        });
      } else {
        ElMessage({
          message: "未注册",
          type: "error",
        });
      }
    });
  }
}
</script>

<style lang="scss" scoped>
body{
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: url(../../assets/bgc.png) no-repeat ;
}
.img {
  width: 400px;
  margin: 240px auto;
  // margin-left: 900px;
  img {
    width: 100%;
  }
}
.login {
  width: 400px;
  height: 300px;
  display: flex;
  align-items: center;
  position: absolute;
  text-align: center;
  left: 50%;
  top: 55%;
  margin-top: -150px;
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid #ccc;
  border-radius: 10px;
  margin-left: -200px;
}
.el-button[data-v-5652d04b]  {
  margin-top: 20px;
  // margin-left: 20px;
  width: 160px;
}
.texiao {
  width: 1000px;
  position: absolute;
  top: 18%;
  left: 38%;
}
</style>